package imsdk

import (
	"encoding/json"
	"longmen/server/config/global"
	"longmen/server/pkg/common/helper/http"
)

type (
	AddGroupMemberRequest struct {
		GroupId    string       `json:"GroupId"`    // 要操作的群组（必填）
		Silence    int          `json:"Silence"`    // 是否静默加人。0：非静默加人；1：静默加人。不填该字段默认为0
		MemberList []MemberList `json:"MemberList"` // 一次最多添加300个成员
	}
	MemberList struct {
		MemberAccount string `json:"Member_Account"` // 要添加的群成员 ID（必填)
		Result        int    `json:"Result"`
	}
	AddGroupMemberResponse struct {
		ActionStatus string       `json:"ActionStatus"`
		ErrorInfo    string       `json:"ErrorInfo"`
		ErrorCode    int          `json:"ErrorCode"`
		MemberList   []MemberList `json:"MemberList"`
	}
)

func (s sdk) AddGroupMember(req AddGroupMemberRequest) (ok bool, resp AddGroupMemberResponse) {
	resp = AddGroupMemberResponse{}
	reqUrl := s.BuildRestfulUrl("/group_open_http_svc/add_group_member")
	bytes, err := json.Marshal(req)
	if err != nil {
		global.Log.Error("request addGroupMember error: ", err)
		return
	}
	result, err := http.HttpPost(reqUrl, string(bytes), connTimeoutMs, serveTimeoutMs, "application/json")
	err = json.Unmarshal(result, &resp)
	if err != nil {
		global.Log.Error("request addGroupMember Unmarshal error: ", err)
		return
	}
	if resp.ActionStatus == FailActionStatus {
		global.Log.Error("request addGroupMember error: ", err, "error message : ", resp.ErrorInfo, "req:", string(bytes))
		return
	}
	ok = true
	return
}
